//
//  LoginViewController.m
//  Hive
//
//  Created by 惠俊贝 on 15/8/31.
//  Copyright (c) 2015年 ZM. All rights reserved.
//

#import "LoginViewController.h"
#import "LeftMenuViewController.h"
#import "MainMapViewController.h"
#import "LeftSlideViewController.h"
#import "ABNetworkUtil.h"
#import "UserAccount.h"
#import "UserDefaultUtil.h"
#import "Customer.h"

@interface LoginViewController () {
    
    __weak IBOutlet UIView *userView;
    __weak IBOutlet UIView *pswdView;
    __weak IBOutlet UIButton *loginButton;
    __weak IBOutlet UIButton *registerButton;
    __weak IBOutlet UIButton *havTryButton;
    __weak IBOutlet UIView *buttomView;
    __weak IBOutlet UITextField *userAccountTF;
    __weak IBOutlet UITextField *passwordTF;
    
    __weak IBOutlet UIButton *rememberPSWDCButton;
    __weak IBOutlet UIButton *autologinCButton;
}

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    self.navigationController.navigationBarHidden = YES;
    customNavBar.hidden = YES;
    
    userView.layer.cornerRadius = 5.0f;
    userView.layer.borderColor = [UIColor whiteColor].CGColor;
    userView.layer.borderWidth = 1.0f;
    userView.layer.masksToBounds = YES;
    
#ifdef TEST
    userAccountTF.text = @"supplier";
    passwordTF.text = @"123456";
    
#else
    userAccountTF.text = @"安泽测试";
    passwordTF.text = @"123456";
#endif
    
    pswdView.layer.cornerRadius = 5.0f;
    pswdView.layer.borderColor = [UIColor whiteColor].CGColor;
    pswdView.layer.borderWidth = 1.0f;
    pswdView.layer.masksToBounds = YES;
    
    
    loginButton.layer.cornerRadius = 5.0f;
    loginButton.layer.borderColor = [UIColor whiteColor].CGColor;
    loginButton.layer.borderWidth = 1.0f;
    loginButton.layer.masksToBounds = YES;
    
    if ([UserDefaultUtil isRememberPSWD]) {
        userAccountTF.text = [UserDefaultUtil getUserAccount];
        passwordTF.text = [UserDefaultUtil getPassword];
        rememberPSWDCButton.selected = YES;
    }
    if ([UserDefaultUtil isAutoLogin]) {
        autologinCButton.selected = YES;
        [self loginButtonPress:nil];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)checkPswdButtonPress:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
    if (!btn.selected) {
        autologinCButton.selected = NO;
    }
}
- (IBAction)checkAutoLoginButtonPress:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
    if (btn.selected) {
        rememberPSWDCButton.selected = YES;
    }
}

- (IBAction)loginButtonPress:(id)sender {
    
    [self showHUBMessage:@"正在登陆..."];
    
    UserAccount *userAccount = [[UserAccount alloc] init];
    userAccount.UserName = userAccountTF.text;
    userAccount.Password = passwordTF.text;

    [UserDefaultUtil setIsRememberPSWD:rememberPSWDCButton.selected];
    [UserDefaultUtil setIsAutoLogin:autologinCButton.selected];
    
    [[[ABNetworkUtil alloc] init]
     loginWithUserAccount:userAccount
     success:^(id response) {
         [self hideHUB];
         NSDictionary *dic = (NSDictionary *)response;
         NSInteger result = [[dic objectForKey:@"Result"] integerValue];
         if (result == 0) {
             // 成功
             
             //用户设置
             [UserDefaultUtil setIsAutoLogin:autologinCButton.selected];
             
             if (rememberPSWDCButton.selected) {
                 [UserDefaultUtil setPassword:passwordTF.text];
             }
             else {
                 [UserDefaultUtil setPassword:@""];
             }
             [UserDefaultUtil setUserAccount:userAccountTF.text];
             
             //用户信息
             [UserDefaultUtil setToken:[dic objectForKey:@"Token"]];
             [UserDefaultUtil setUserID:[dic objectForKey:@"UserID"]];
             [UserDefaultUtil setTempQty:[dic objectForKey:@"TempQty"]];
             [UserDefaultUtil setSystem:[dic objectForKey:@"System"]];
             [UserDefaultUtil setAleaQty:[dic objectForKey:@"AleaQty"]];
             [UserDefaultUtil setUserName:[dic objectForKey:@"UserName"]];
//             [UserDefaultUtil setCustomer:[Customer creatCustomerWithDic:[dic objectForKey:@"Response_Customer"]]];
             
             LeftMenuViewController *leftController = [[LeftMenuViewController alloc] initWithNibName:@"LeftMenuViewController" bundle:nil];
             
             
             MainMapViewController *mainController = [[MainMapViewController alloc] init];
             
             LeftSlideViewController *homeVC = [[LeftSlideViewController alloc] initWithLeftView:leftController andMainView:mainController];
             [self.navigationController pushViewController:homeVC  animated:YES];
             [UIView showToast:@"登陆成功"];
         }
         else {
             NSLog(@"resutl=%d:%@",(int)result,[dic objectForKey:@"ErrorMsg"]);
             [UIView showToast:[dic objectForKey:@"ErrorMsg"]];
         }
    }
     fail:^(NSError *error) {
         [self hideHUB];
         NSLog(@"%@",error.localizedDescription);
    }];
}
- (IBAction)forgetButtonPress:(id)sender {
    
    UIAlertView *forgetAlert = [[UIAlertView alloc] initWithTitle:nil message:@"如果您的登录密码忘记了，可以找销售给您设备的服务商，重置一下密码。密码被重置后，会自动恢复为123456" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [forgetAlert show];
}
- (IBAction)newAccoutButtonPress:(id)sender {
    
}
- (IBAction)tryButtonPress:(id)sender {
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
